﻿using System.Collections;
using System.Collections.Generic;
using UnityEngine;

[CreateAssetMenu(fileName = "Straight", menuName = "BulletMotion/Straight")]
public class Straight : BulletMotion
{
    [Tooltip("初速度")]
    public float velocity;
    [Tooltip("加速度")]
    public float accelaration;
    [Tooltip("最大速度(应该总是正值)")]
    public float maxSpeed;

    /// <summary>
    /// 新建直线轨迹。
    /// </summary>
    /// <param name="v">初速度</param>
    /// <param name="a">加速度</param>
    /// <param name="mv">最大速度(应该总是正值)</param>
    public Straight(float awake, float life, float v, float a, float mv)
    {
        awakeTime = awake;
        lifeTime = life;
        velocity = v;
        accelaration = a;
        maxSpeed = mv;
    }

    public override Vector2 Motion(float time)
    {
        if (awake)
        {
            time -= awakeTime;
            Vector2 targetMovement;
            Vector2 pos = (velocity + accelaration * time) * TowardsDir.normalized * Time.fixedDeltaTime;
            if ((accelaration > 0 ? 1 : -1) * pos.magnitude > maxSpeed)
            {
                targetMovement = (accelaration > 0 ? 1 : -1) * TowardsDir.normalized * maxSpeed * Time.fixedDeltaTime;
            }
            else
            {
                targetMovement = pos;
            }
            return targetMovement;
        }
        else
        {
            return Vector2.zero;
        }
    }
}
